"""
@Used to create user table present in Mysql file 
"""

import xml.etree.cElementTree as ET
import MySQLdb
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

#"""
site_file = "temp_sites"#"all_site_name"
table_type = "users"
file_path = "D:\Research\ISEC-2014\data\content"
database = "isec2014"
user = "root"
password="123"
port=3306

"""
site_file = "all_site_name"
table_type = "users"
file_path = "E:\Sangeeta\dataset\\2013_05_stack_overflow\Content"
database = "sangeeta"
user = "sangeetal"
password="sangeetal"
port=3307
#"""

db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
insert_cursor = db1.cursor() 

file = open(site_file)
for line in file:
    site = line.strip() +".com"
    print  "site=",site
    table  = table_type+"_"+line.strip()+"_aug13"
    tree = ET.ElementTree(file=file_path+"\\"+site+"\\"+table_type+".xml")
    root= tree.getroot()
    #print root.tag, root.attrib
    count = 0
    for child_of_root in root:
        #print  child_of_root.attrib
       
        try:
            email_hash  = (child_of_root.attrib['EmailHash'])
        except Exception, e:
            email_hash = "-1"  
        #print "val=", val  
        try: 
            web_url = (str)(child_of_root.attrib['WebsiteUrl'])
        except Exception, e:
            web_url = "-1"  
      
       
        
        try: 
            age = (str)(child_of_root.attrib['Age'])
        except Exception, e:
            age = "-1"  
        
        try: 
            location = (str)(child_of_root.attrib['Location'])
        except Exception, e:
            location = "-1"
         
        location = location.replace("\n"," ")
        location = location.replace("'"," ")   
        location = location.replace(","," ")  
            
        try: 
            about_me = (str)(child_of_root.attrib['AboutMe'])
        except Exception, e:
            about_me = "-1"        
        about_me = about_me.replace("\n"," ")
        about_me = about_me.replace("'"," ")
        about_me = about_me.replace("\""," ")
        about_me = about_me.replace(",", " ")
        about_me = about_me.replace("(", " ")
        about_me =about_me.replace(")", " ")
        about_me = about_me.replace("\\", " ")    
        
        display_name = (str)(child_of_root.attrib['DisplayName']).replace("'"," ")      
            
        insert_str = "insert into `"+ table+ "` values("+child_of_root.attrib['Id']+","+child_of_root.attrib['Reputation']+\
        ",'"+child_of_root.attrib['CreationDate']+"','"+ display_name+"','"+\
        child_of_root.attrib['LastAccessDate']+"','"+web_url+"','"+\
        about_me+"','"+location+"',"+\
        child_of_root.attrib['Views']+","+child_of_root.attrib['UpVotes']+","+child_of_root.attrib['DownVotes']\
        +",'"+email_hash+"',"+age+")"
         
        #print "insert = ", insert_str
        insert_cursor.execute(insert_str)
        #break
    db1.commit()
    print "Number of errors = ", count


